package com.psjj.privilege.service;

import com.psjj.privilege.dao.RoleDao;
import com.psjj.privilege.po.Role;

import java.sql.SQLException;
import java.util.List;

public class RoleService {
    private RoleDao roleDao = new RoleDao();

    public List<Role> queryRoles() {
        List<Role> list = null;
        try {
            list = roleDao.queryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void add(Role role) {
        try {
            roleDao.add(role);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Role queryRoleById(int rid) {
        Role role = null;
        try {
            role = roleDao.queryRoleById(rid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return role;
    }

    public void editRole(Role role) {
        try {
            roleDao.edit(role);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void del(int rid) {
        try {
            roleDao.del(rid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<Role> queryRolesJoinRoleStatus(int uid) {
        //获得所有角色信息
        List<Role> roles = queryRoles();
        //关联当前用户所拥有的角色的状态信息
        roles.forEach(role->{
            try {
                boolean tag = roleDao.queryHasUserIdAndRoleId(uid,role.getRid());
                if(tag){
                    role.setCheck(true);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        });
        return roles;

    }
}
